$(document).ready(function(){
    init();
});


function init(){
    var tabID = "#loginOuter"; //if you change this, change in HTML too
        
    $(tabID).tabs(); //creates tab
    $(tabID).resizable({
        minHeight: 500,
        minWidth: 400,
        
    });
	window.errorMark = $('.errorMark');
	window.check = $('.checkbox');
    $('#id_username').change(function(){checkUsername();})
    if ($('#notifyFail').length != 0){
    	$.notifyBar({
    		html: 'Login Failed either Username or password was incorrect',
    		delay: 1000,
    		animationSpeed: "normal",
    		cls: "error"
    	});
    }
}

function validateForm(){
	checkUsername()
	if (okToSubmit & ($('#id_username').val()!='')){
		return true;
	} else {
		$.notifyBar({
			html: 'Choose another username, this one already exists',
			delay: 2000,
			animationSpeed: "normal",
			cls: "error"
		});
		return false;
	}
}

function checkUsername(notify){
	if ($('#id_username').val()!=''){
	$.ajax({url:$('#id_username').parents('form').attr('url') + $('#id_username').val(),
		type: "GET",
		data: '',
		dataType: 'json',
		success:function(info){
			
			if (info['success']){
				$('#id_username').next('.image').remove();
				$('#id_username').after(check.clone());
				okToSubmit = true;
			} 
			else {
				$('#id_username').next('.image').remove();
				$('#id_username').after(errorMark.clone());
				okToSubmit = false		
			}
		},
		error:function(){
			if (notify){
			$.notifyBar({
				html: 'An error occured while sending the data',
				delay: 2000,
				animationSpeed: "normal",
				cls: "error"
			});
			}
		}
	});
	}
}